package com.foreknow.service;

import com.foreknow.bean.GuestBook;
import com.foreknow.dao.GuestBookDao;
import com.foreknow.dao.impl.GuestBookDaoImpl;
import com.foreknow.db.DBManager;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GuestBookServiceImpl implements GuestBookService {
    @Override
    public boolean addBook(GuestBook guestBook) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestBookDaoImpl guestBookDao = new GuestBookDaoImpl();
        guestBookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestBookDao.addBook(guestBook);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    @Override
    public boolean deleteBook(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestBookDaoImpl guestBookDao = new GuestBookDaoImpl();
        guestBookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestBookDao.deleteBook(id);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }

        return isRight;
    }

    @Override
    public GuestBook selectById(Integer id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestBookDaoImpl guestBookDao = new GuestBookDaoImpl();
        guestBookDao.setConnection(conn);
        GuestBook guestBook = new GuestBook();
        try {
            guestBook = guestBookDao.selectById(id);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return guestBook;
    }

    @Override
    public boolean updateBook(GuestBook guestBook, String name, String phone, String email, String title, String content, String createdTime) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestBookDaoImpl guestBookDao = new GuestBookDaoImpl();
        guestBookDao.setConnection(conn);
        boolean isRight = false;
        try {
            isRight = guestBookDao.updateBook(guestBook, name, phone, email, title, content ,createdTime);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return isRight;
    }

    @Override
    public List<Object> showAllBook() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestBookDaoImpl guestBookDao = new GuestBookDaoImpl();
        guestBookDao.setConnection(conn);
        List<Object>list = new ArrayList<>();
        try {
            list = guestBookDao.showAllBook();
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return list;
    }


    @Override
    public List<Object> selectBook(String name) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestBookDaoImpl guestBookDao = new GuestBookDaoImpl();
        guestBookDao.setConnection(conn);
        List<Object>list = new ArrayList<>();
        try {
            list = guestBookDao.selectBook(name);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return list;
    }

    public static void main(String[] args) {
//        GuestBookServiceImpl gbsl = new GuestBookServiceImpl();
//        GuestBook guestBook = new GuestBook();
//        guestBook.setName("qwer");
//        guestBook.setEmail("123@");
//        guestBook.setPhone("7777");
//        boolean isRight = gbsl.addBook(guestBook);
//        System.out.println(isRight);
//
//        isRight = gbsl.updateBook(13,"title","'UIOP'");
//        System.out.println(isRight);

//        List<GuestBook>list = gbsl.selectBook("a");
//        Iterator<GuestBook> iterator = list.iterator();
//        while (iterator.hasNext()){
//            GuestBook result = iterator.next();
//            System.out.println(result.getID()+" "+result.getName()+" "+result.getCreatedtime());
//        }

//        gbsl.deleteBook(16);
//        gbsl.deleteBook(15);
//        gbsl.deleteBook(14);
    }
}
